import { basicInfo_API } from '@/server/home'
import { defineStore } from 'pinia'

// user
export const useUserStore = defineStore('user', {
    state: () => ({
        token: undefined, // 登录凭证
        userInfo: <any>{}, // 用户基本信息
        isLogin: false, // 是否登录
    }),
    getters: {},
    actions: {
        async getUserInfo() {
            const res = await basicInfo_API()
            if (res.IsSuccess) {
                this.userInfo = res.Data
                if (res.Data.UserType == 9) {
                    uni.reLaunch({
                        url: '/pages/guest/guest',
                    })
                } else {
                    uni.reLaunch({
                        url: '/pages/tabbar/home/home',
                    })
                }
            }
        },
        clearUserAll() {
            this.token = undefined
            this.isLogin = false
            this.userInfo = {}
            uni.removeStorageSync('user')
        },
    },
    persist: {
        storage: {
            getItem(key) {
                return uni.getStorageSync(key)
            },
            setItem(key, value) {
                uni.setStorageSync(key, value)
            },
        },
    },
})
